package com.miplus.ccrm.workflow.security;

import com.miplus.ccrm.common.JDataService;
import com.miplus.ccrm.common.JPubUtils;
import com.miplus.ccrm.common.JSysUser;
import org.springframework.stereotype.Component;
import org.springframework.web.context.request.RequestContextHolder;
import org.springframework.web.context.request.ServletRequestAttributes;

import javax.annotation.Resource;
import javax.servlet.http.HttpServletRequest;
import java.util.Objects;

@Component
public class JCommonData {

    @Resource
    private JDataService dataService;

    public String getCurrUsrID() {
        HttpServletRequest h = ((ServletRequestAttributes) Objects.requireNonNull(RequestContextHolder.getRequestAttributes())).getRequest();
        String token = JPubUtils.getToken(h);
        String key = JPubUtils.getTokenKey(token);
        JSysUser user = (JSysUser) dataService.get(key);
        return user.getUsrid();
    }

    public JSysUser getCurrUsr() {
        HttpServletRequest h = ((ServletRequestAttributes) Objects.requireNonNull(RequestContextHolder.getRequestAttributes())).getRequest();
        String token = JPubUtils.getToken(h);
        String key = JPubUtils.getTokenKey(token);
        return (JSysUser) dataService.get(key);
    }

    public String getDistQuyKey(String usrid) {
        return "distcrdcasequyparas_" + usrid;
    }

    public String getRecyQuyKey(String usrid) {//2024-07-28
        return "recycrdcasequyparas_" + usrid;
    }

    public String getAssQuyKey(String usrid) {
        return "asscrdcasequyparas_" + usrid;
    }

    public String getDistResultKey(String usrid) {
        return "distcrdcaseresult_" + usrid;
    }

    public String getAssResultKey(String usrid) {
        return "asscrdcaseresult_" + usrid;
    }

    public String getQuyNextCaseKey(String usrid) {//2024-10-30
        return "nextcasequyparas_" + usrid;
    }

    public String getQuyNextCaseDataKey(String usrid) {//2024-10-30
        return "nextcasequydata_" + usrid;
    }

    public boolean setData(String key, Object d, long time) {
        return dataService.set(key, d, time);
    }

    public Object getData(String key) {
        if (dataService.hasKey(key)) {
            return dataService.get(key);
        }
        return null;
    }

    public void delData(String key) {
        if (dataService.hasKey(key)) {
            dataService.del(key);
        }
    }

    public void SetUsrSmsVCode(String usrid, String code) {
        String keyid = JPubUtils.getVCodeIden(usrid);
        dataService.set(keyid, code, JPubUtils.vcodeExpiration);
    }


    //当前正在进线批量回收处理中..
    public boolean IsRecycleProcess() {
        String key = JPubUtils.recvProcessKey;
        if (dataService.hasKey(key)) {
            Integer count = (Integer) dataService.get(key);
            if (count != null) return true;
        }
        return false;
    }

    public void SetRecycleProcess(Integer count) {
        String key = JPubUtils.recvProcessKey;
        dataService.set(key, count, 1800);
    }

    public Integer GetRecycleProcessCount() {
        Integer count = -1;
        String key = JPubUtils.recvProcessKey;
        if (dataService.hasKey(key)) {
            count = (Integer) dataService.get(key);
        }
        return count;
    }

    public void RemoveRecycleProcess() {
        String key = JPubUtils.recvProcessKey;
        delData(key);
    }
}
